/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include "sle_lite_tool_announce.h"


void OnSleTAnnounceStateChangeCb(uint8_t announceId, SleAnnounceState announceState, bool isTerminaled)
{
    SLE_TEST_TOOL_LOG("[SleAnnounceTestTool] OnSleTAnnounceStateChangeCb announceId:%d, announceState:%s(%hhu), isTerminaled:%d"
        , announceId, SleAnnounceStateStr(announceState), announceState, isTerminaled);
}

static uint8_t g_announceId = 255;
void TAddAnnounce(void)
{
    SleErrorCode ret = AddAnnounce(&g_announceId);
    if(ret != SLE_NO_ERROR) {
        printf("[SleAnnounceTestTool] AddAnnounce failed, ret:%hhu\r\n", ret);
        return;
    }
    SLE_TEST_TOOL_LOG("[SleAnnounceTestTool] AddAnnounce success, announceId:%hhu", g_announceId);
}

void TRemoveAnnounce(void)
{
    SleErrorCode ret = RemoveAnnounce(g_announceId);
    if(ret != SLE_NO_ERROR) {
        printf("[SleAnnounceTestTool] RemoveAnnounce failed, ret:%hhu\r\n", ret);
        return;
    }
    g_announceId = 255;
    SLE_TEST_TOOL_LOG("[SleAnnounceTestTool] RemoveAnnounce success, announceId:%hhu", g_announceId);
}

void TStartAnnounce(void)
{
    char input[184]; // SleGetInputString识别最后一个结束符，只截取183
    char announceData[201] = {'k','h','_','s','l','e','_','t','o','o','l','_','d','a','t','a',':', '\0'}; // 只存储前200字节
    SleGetInputString("Please input announceData", input, 184);
    strcat(announceData + 17, input);
    char responceData[201] = {'k','h','_','s','l','e','_','t','o','o','l','_','r','e','s','p',':', '\0'};
    SleGetInputString("Please input responseData", input, 184);
    strcat(responceData + 17, input);
    SleAnnounceData data = {
        .announceLength = strlen(announceData),
        .announceData = (uint8_t*)announceData,
        .responceLength = strlen(responceData),
        .responceData = (uint8_t*)responceData,
    };
    SleErrorCode ret = StartAnnounce(g_announceId, &data, NULL);
    if(ret != SLE_NO_ERROR) {
        printf("[SleAnnounceTestTool] StartAnnounce failed, ret:%hhu\r\n", ret);
        return;
    }
    SLE_TEST_TOOL_LOG("[SleAnnounceTestTool] StartAnnounce success, announceId:%hhu", g_announceId);
}

void TStopAnnounce(void)
{
    SleErrorCode ret = StopAnnounce(g_announceId);
    if(ret != SLE_NO_ERROR) {
        printf("[SleAnnounceTestTool] StopAnnounce failed, ret:%hhu\r\n", ret);
        return;
    }
    SLE_TEST_TOOL_LOG("[SleAnnounceTestTool] StopAnnounce success, announceId:%d\r\n", g_announceId);
}

void TGetDefaultAnnounceParamConfig(void)
{
    SleAnnounceParam announceParam;
    SleErrorCode ret = GetDefaultAnnounceParamConfig(&announceParam);
    if(ret != SLE_NO_ERROR) {
        printf("[SleHostTestTool] GetDefaultAnnounceParamConfig failed, ret:%hhu\r\n", ret);
        return;
    }
    printf("[SleHostTestTool] handle:%hhu\r\n", announceParam.handle);
    printf("[SleHostTestTool] mode:%hhu\r\n", announceParam.mode);
    printf("[SleHostTestTool] role:%hhu\r\n", announceParam.role);
    printf("[SleHostTestTool] level:%hhu\r\n", announceParam.level);
    printf("[SleHostTestTool] annonceIntervalMin:%u\r\n", announceParam.annonceIntervalMin);
    printf("[SleHostTestTool] annonceIntervalMax:%u\r\n", announceParam.annonceIntervalMax);
    printf("[SleHostTestTool] channelMap:%hhu\r\n", announceParam.channelMap);
    printf("[SleHostTestTool] txPower:%hhd\r\n", announceParam.txPower);
    printf("[SleHostTestTool] connectIntervalMin:%hu\r\n", announceParam.connectIntervalMin);
    printf("[SleHostTestTool] connectIntervalMax:%hu\r\n", announceParam.connectIntervalMax);
    printf("[SleHostTestTool] connectLatency:%hu\r\n", announceParam.connectLatency);
    printf("[SleHostTestTool] connectTimeout:%hu\r\n", announceParam.connectTimeout);
}

void SleAnnounceToolHalper(void)
{
    printf("****SleAnnounceTool Command List*****\r\n");
    for (uint16_t i = 0; i < g_announceCmdNum; i++) {
        printf("*     %02d - %-20s     *\r\n", i, g_announceCmdList[i].desc);
    }
    printf("*************************************\r\n");
}

void SleAnnounceToolCmdHandler(int argc, char **argv)
{
    printf("SleAnnounceToolCmdHandler in\r\n");
    //试配uart接口的调用方式
    SleResetEnv();
    //从函数名字之后开始处理参数列表
    SleSetBufEnv(argc, argv, 1);
    int index = SleGetInputNumber("Please input cmd index:\r\n");
    if(index < g_announceCmdNum) {
        printf("cmd: %d\r\n", index);
        printf("Execute: %s\r\n", g_announceCmdList[index].desc);
        g_announceCmdList[index].func();
    }
    if(index != 0) g_announceCmdList[0].func();
}

#ifdef __cplusplus
}
#endif // __cplusplus